import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';

import { SkWebService } from '@service/skweb.service';

@Component({
  selector: 'app-allaccount',
  templateUrl: './allaccount.component.html',
  styles: []
})
export class AllaccountComponent implements OnInit {
  public accountList: any[] = [];
  public dataTotal = 1;  // 数据总量
  public pageIndex = 1;  // 页码
  public pageSize = 10;  // 每页数量

  public loading = true; // 数据加载loading

  public disabledButton = true; // 是否隐藏重新创建按钮选项
  public checkedNumber = 0; // 选择数量
  public allDisabled = false;  // 标题栏中全部选中按钮的是否可选状态
  public allChecked = false;  // 是否全部选中
  public indeterminate = false;  // checkbox不确定状态
  public queryStr;  // 模糊查询内容
  public selectBillNoList = {}; // 选中的列表(记录动态分页的选中的列表)

  constructor(
    public msg: NzMessageService,
    private skWebService: SkWebService
  ) { }

  public ngOnInit() {
    this.fetchAccountList();
  }

  // 获取账号数据
  public async fetchAccountList(reset: boolean = false) {
    if (reset) {
      this.pageIndex = 1;
      this.queryStr = null;
    }
    // 数据参数
    const options: any = {
      pageIndex: this.pageIndex,
      pageSize: this.pageSize
    };
    if (this.queryStr) {
      options.accountName = this.queryStr.trim();
    }
    this.loading = true;  // 数据加载loading
    const ret: any = await this.skWebService.fetchEosAccount(options); // 查询数据
    this.loading = false; // 关闭数据加载loading

    const { count, rows } = ret.data;
    this.dataTotal = count;  // 数据总数
    this.accountList = rows; // 数据
    // 修改checkbox状态
    // 将已选中的选项修改显示选中状态
    this.accountList.forEach(data => data.checked = this.selectBillNoList[data.billNo]);
    if (this.accountList.length > 0) {
      // 判断标题栏中全部选中按钮的是否可选, 若数据都为complete, 则为不可选状态
      this.allDisabled = this.accountList.every(value => value.status === 'complete');
      // 判断是否全部选中或者是否全部未选中
      const allChecked = this.accountList.every(value => this.selectBillNoList[value.billNo] === true);
      const allUnChecked = this.accountList.every(value => !this.selectBillNoList[value.billNo]);

      // 修改全部选择状态
      this.allChecked = allChecked;
      console.log('this.allChecked=====', this.allChecked);
      this.indeterminate = (!allChecked) && (!allUnChecked);
    } else {
      this.allChecked = false;
      this.indeterminate = false;
    }
  }

  // 重新加入创建账号队列
  public async confirmRecreate(billNo?: string) {
    this.loading = true;  // 数据加载loading
    const billNoList = [];
    // 判断是否有直接传参
    if (billNo) {
      billNoList.push(billNo);
    } else {
      // 统计选中状态下的数据
      // tslint:disable-next-line:forin
      for (const key in this.selectBillNoList) {
        if (this.selectBillNoList[key]) {
          billNoList.push(key);
        }
      }
    }
    // 若无选中的数据, 不触发api
    if (billNoList.length <= 0) {
      return;
    }
    const ret: any = await this.skWebService.recreateAccount(billNoList);
    this.loading = false;  // 数据加载loading
    if (ret && !ret.errcode) {
      // 重新创建后清空选中信息, 重置重新创建按钮状态和选中数量
      this.selectBillNoList = {};
      this.disabledButton = true;
      this.checkedNumber = 0;
      this.msg.success('成功将已选择的账号加入创建队列中, 稍后请刷新数据重新查看账号状态');
      this.fetchAccountList();
    }
  }

  // 选中全部
  public checkAll(value: boolean): void {
    this.accountList.forEach(data => {
      if (data.status !== 'complete') {
        this.selectBillNoList[data.billNo] = value;
      }
    });
    this.refreshStatus();
  }

  // 更新选中信息
  public refreshStatus() {
    // 判断是否全部选中或者是否全部未选中
    const allChecked = this.accountList.every(value => this.selectBillNoList[value.billNo] === true);
    const allUnChecked = this.accountList.every(value => !this.selectBillNoList[value.billNo]);
    // 修改全部选择状态
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);

    // 统计选中的个数
    let selectNumber = 0;
    // tslint:disable-next-line:forin
    for (const billNo in this.selectBillNoList) {
      selectNumber += this.selectBillNoList[billNo] ? 1 : 0;
    }
    // 统计选中的个数
    this.checkedNumber = selectNumber;
    // 有选中时显示重新创建的按钮
    this.disabledButton = this.checkedNumber ? false : true;
  }
}
